#include "lvgl/lvgl.h"

#include <math.h>
#include <stdio.h>

#include "../my_inc/shapes.h"
#include "../my_inc/canvas.h"


shape_type_t current_shape = SHAPE_NULL;
lv_color_t   shape_color   = LV_COLOR_MAKE(0xFF, 0x00, 0x00);
lv_point_t   start_pt      = {0};


void canvas_press_cb(lv_event_t *e)
{
    printf("canvas_press_cb\n");
    lv_obj_t *c = lv_event_get_target(e);
    lv_indev_get_point(lv_indev_get_act(), &start_pt);

    /* 坐标转换：画布局部 */
    lv_area_t a;
    lv_obj_get_coords(c, &a);
    start_pt.x -= a.x1;
    start_pt.y -= a.y1;

}

void canvas_release_cb(lv_event_t *e)
{
    printf("canvas_release_cb\n");

    lv_obj_t *c = lv_event_get_target(e);
    lv_point_t end_pt;
    lv_indev_get_point(lv_indev_get_act(), &end_pt);

    /* 坐标转换 */
    lv_area_t a;
    lv_obj_get_coords(c, &a);
    end_pt.x -= a.x1;
    end_pt.y -= a.y1;

    draw_shape(c, start_pt, end_pt);
}

void draw_shape(lv_obj_t *c, lv_point_t p1, lv_point_t p2)
{
    switch (current_shape) {
        case SHAPE_LINE:
            {
                lv_point_t pts[] = {p1, p2};
                lv_draw_line_dsc_t dsc;
                lv_draw_line_dsc_init(&dsc);
                dsc.color = shape_color;
                dsc.width = pen_size;
                dsc.opa   = LV_OPA_COVER;
                lv_canvas_draw_line(c, pts, 2, &dsc);
            }
            break;

        case SHAPE_RECT:
            {
                lv_area_t area = {
                    .x1 = LV_MIN(p1.x, p2.x),
                    .y1 = LV_MIN(p1.y, p2.y),
                    .x2 = LV_MAX(p1.x, p2.x),
                    .y2 = LV_MAX(p1.y, p2.y)
                };
                lv_draw_rect_dsc_t dsc;
                lv_draw_rect_dsc_init(&dsc);
                dsc.border_color = shape_color;
                dsc.border_width = pen_size;
                dsc.bg_opa       = LV_OPA_TRANSP;
                lv_canvas_draw_rect(c, area.x1, area.y1,
                                    lv_area_get_width(&area),
                                    lv_area_get_height(&area), &dsc);
            }
            break;

        case SHAPE_CIRCLE:
            {
                int dx = p2.x - p1.x;
                int dy = p2.y - p1.y;
                int r = (int)sqrtf(dx*dx + dy*dy);
                lv_draw_arc_dsc_t dsc;
                lv_draw_arc_dsc_init(&dsc);
                dsc.color = shape_color;
                dsc.width = pen_size;
                dsc.opa   = LV_OPA_COVER;
                lv_canvas_draw_arc(c, p1.x, p1.y, r, 0, 360, &dsc);
            }
            break;

        case SHAPE_STAR:
            {
                int dx = p2.x - p1.x;
                int dy = p2.y - p1.y;
                int size = (int)sqrtf(dx*dx + dy*dy);

                const lv_point_t base[5] = {
                    {0, -size},
                    { size*0.951f, -size*0.309f},
                    { -size*0.588f,  size*0.809f},
                    { size*0.588f,  size*0.809f},
                    { -size*0.951f, -size*0.309f}
                };
                lv_point_t pts[5];
                for (int i = 0; i < 5; ++i) {
                    pts[i].x = p1.x + base[i].x;
                    pts[i].y = p1.y + base[i].y;
                }
                lv_draw_rect_dsc_t dsc;
                lv_draw_rect_dsc_init(&dsc);
                dsc.bg_color = shape_color;
                dsc.bg_opa   = LV_OPA_COVER;
                lv_canvas_draw_polygon(c, pts, 5, &dsc);
            }
            break;

        case SHAPE_ARROW:
            {
                /* 直线 */
                lv_point_t pts[] = {p1, p2};
                lv_draw_line_dsc_t line_dsc;
                lv_draw_line_dsc_init(&line_dsc);
                line_dsc.color = shape_color;
                line_dsc.width = pen_size;
                line_dsc.opa   = LV_OPA_COVER;
                lv_canvas_draw_line(c, pts, 2, &line_dsc);

                /* 箭头尖端：小三角 */
                int dx = p2.x - p1.x;
                int dy = p2.y - p1.y;
                float len = sqrtf(dx*dx + dy*dy);
                if (len == 0) break;
                int vx = dx * 10 / len;
                int vy = dy * 10 / len;
                lv_point_t arr[3] = {
                    p2,
                    {p2.x - vx + vy/2, p2.y - vy - vx/2},
                    {p2.x - vx - vy/2, p2.y - vy + vx/2}
                };
                lv_draw_rect_dsc_t poly_dsc;
                lv_draw_rect_dsc_init(&poly_dsc);
                poly_dsc.bg_color = shape_color;
                poly_dsc.bg_opa   = LV_OPA_COVER;
                lv_canvas_draw_polygon(c, arr, 3, &poly_dsc);
            }
            break;

        case SHAPE_NULL:
            break;
    }

    printf("shape: %d",current_shape);
}
